package com.zq.it.dao.impl.admin;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.zq.it.dao.admin.CourseManageDao;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.CourseManage;

public class CourseManageDaoImpl extends BaseDao<CourseManage> implements CourseManageDao {
  /*public static final Logger logger=t(CourseManageDaoImpl.class);*/
	@Override
	public void saveCourseManageEty(CourseManage cmg){		
    	getHibernateTemplate().save(cmg);
	}
	@Override
	public void deleteCourseManageEtyByID(Integer id){	
		deleteEntity("delete CourseManage as p where p.courseManageId="+id);
	}

	@Override
	public List findCourseManageEtyList( final Integer id, final  String name){
		return getHibernateTemplate().executeFind(new HibernateCallback(){
			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
					if(id!=null&&!name.equals(null))
					  return arg0.createCriteria(CourseManage.class).add(org.hibernate.criterion.Restrictions.idEq(id)).setFetchMode("course",org.hibernate.FetchMode.JOIN).list();				    								  
			 return null;
			}						
		});
	}

	@Override
	public List findCourseManageEtyById(Integer id){
		return getHibernateTemplate().find("from CourseManage where courseManageId=?",id);
		// TODO Auto-generated method stub	
	}

	@Override
	public List findCourseManageEtyByName(String name) {
		return getHibernateTemplate().find("from CourseManage as p where p.courseManageName=?",name);
		// TODO Auto-generated method stub
	
	}

//	@Override
//	public void updateCourseManageByID(Integer id) {
//		getHibernateTemplate().update(entity);
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	public void updateCourseManageEty(CourseManage courseManage) {
		getHibernateTemplate().update(courseManage);
		// TODO Auto-generated method stub
		
	}
//	@Override
//	public List findCourseManageEtyByDate(Date date){
//		// TODO Auto-generated method stub
//		return getHibernateTemplate().find("from CourseManage where createTime=?",date);
//	}
	@Override
	public List findCourseManageEtyByExmp(CourseManage courseManage) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByExample(courseManage);
	}
	@Override
	public void deleteAllEty(java.util.Collection<CourseManage> collection){
		getHibernateTemplate().deleteAll(collection);
	}
	@Override
	public void deleteCourseManageEtyByObj(CourseManage courseManage){
		getHibernateTemplate().delete(courseManage);
	}
	@Override
	public void saveCourseManageEtyList(Collection<CourseManage> collection){
		getHibernateTemplate().saveOrUpdateAll(collection);
	}
	@Override
	public List findCourseManageListByHQL(String hql){
		// TODO Auto-generated method stub
		return findEtyByHql(hql);
	}
	/*@Override
	public List findCourseManageAllEty(){	
		
		return getHibernateTemplate()
				.find("select "+
						"new com.zq.it.pojo.CmgTemp(cmg.id,cmg.courseTimeSet,cmg.weeks,cmg.courseType,cmg.credit,"+
						"t.teacherId,t.teacherName,"+
						"c.courseId,c.courseName,"+
						"adr.addressId,adr.addressName,"+
						"tc.classId,tc.className)"+
						"from CourseManage cmg"+
						" left join cmg.teacher t"+
						" left join cmg.course c"+
						" left join cmg.address adr"+
						" left join cmg.tclass tc");									
	}
	*/
	
	@Override
	public List findCourseManageListBySQL(String sql){
		// TODO Auto-generated method stub
		return findEtyBySQL(sql);
	}
	@Override
	public List findCourseManageListEtyByFetchMode(String sql){
		return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(CourseManage.class).add(Restrictions.sqlRestriction(sql))
				.setFetchMode("course",FetchMode.JOIN)
				.setFetchMode("teacher",FetchMode.JOIN)
				.setFetchMode("address",FetchMode.JOIN)
				.setFetchMode("tclass",FetchMode.JOIN));		
	}
	@Override
	public void deleteCourseManageEtys(int[] ids){
		for(int i=0;i<ids.length;i++)
			getHibernateTemplate().delete(getHibernateTemplate().load(CourseManage.class,ids[i]));	
		// TODO Auto-generated method stub
		
	}
	
}
